﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using WinFormsAppTest.Dao;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WinFormsAppTest
{
    public partial class FilesAddForm : Form
    {
        //
        public String username = "null";//设置默认登录用户(手动添加)
        public int id = 0;//设置默认登录用户(手动添加)
        //
        public FilesAddForm()
        {
            InitializeComponent();
        }

        // -------有参 窗口（手动添加）--------
        public FilesAddForm(String name, int uid)
        {
            username = name;
            id = uid;
            InitializeComponent();
        }
        //-----------------------------------

        private void OKbutton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(MsgtextBox.Text))
            {
                MessageBox.Show("添加员工基本信息不能为空！");
            }
            else if (string.IsNullOrWhiteSpace(HousetextBox.Text))
            {
                MessageBox.Show("添加员工户口信息不能为空！");
            }
            else if (string.IsNullOrWhiteSpace(ContracttextBox.Text))
            {
                MessageBox.Show("添加员工合同管理不能为空！");
            }
            else if (string.IsNullOrWhiteSpace(InsuretextBox.Text))
            {
                MessageBox.Show("添加员工投保情况不能为空！");
            }
            else if (string.IsNullOrWhiteSpace(UserIdtextBox.Text))
            {
                MessageBox.Show("添加员工编号不能为空！");
            }

            AdminDao testdao = new AdminDao();
            FilesDao filesdao = new FilesDao();
            if(int.TryParse(UserIdtextBox.Text, out int number)
                && testdao.GetUserById(number).Count !=0)
            { 
                //直接调用dao层进行添加
                //TestDao testdao = new TestDao();
                if(filesdao.GetFilesByUserId(number).Count ==0)
                {

                    filesdao.CreateFiles(MsgtextBox.Text, HousetextBox.Text,
                                         ContracttextBox.Text, InsuretextBox.Text,
                                         number);
                    MessageBox.Show("添加成功！");
                    FilesForm f2 = new FilesForm(username, id);
                    this.Hide();
                    f2.ShowDialog();
                    this.Dispose();
                }
                else
                {
                    // 添加一个OKCancel类的弹窗进行确认
                    DialogResult result= MessageBox.Show("该员工档案已经存在，是否继续添加覆盖之前的档案信息","确认", 
                                    MessageBoxButtons.OKCancel,
                                    MessageBoxIcon.Question);
                    if(result == DialogResult.OK)
                    {

                        filesdao.DeleteFilesByUserId(number);
                        filesdao.CreateFiles(MsgtextBox.Text, HousetextBox.Text,
                                        ContracttextBox.Text, InsuretextBox.Text,
                                        number);
                        MessageBox.Show("覆盖成功！");
                        FilesForm f2 = new FilesForm(username, id);
                        this.Hide();
                        f2.ShowDialog();
                        this.Dispose();
                    }
                }

            }
            else
            {
                MessageBox.Show("添加员工编号不存在！");
            }
        }

        private void Exitbutton_Click(object sender, EventArgs e)
        {
            FilesForm f2 = new FilesForm(username, id);
            this.Hide();
            f2.ShowDialog();
            this.Dispose();
        }

        private void FilesAddForm_Closing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                // 关闭进程
                Process.GetCurrentProcess().Kill();
            }
        }
    }
}
